<?php 
header("Content-Type:text/html;charset=utf-8");
session_start();
include "includes/init.php";
error_reporting(E_ERROR | E_PARSE);
    global $result;
    $time = time();
    //产品信息
    $phone=isset($_SESSION['phone'])?$_SESSION['phone']:'';
    $mid=isset($_SESSION['id'])?$_SESSION['id']:'';
    if(isset($_GET['usertype']) == 1){
        $sql = "SELECT DISTINCT
               p.type
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.mifeng = 0
                    AND p.type = b.btype
                    AND b.mid = m.id
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < '".$time."'
                AND p.type = b.btype
                AND b.mid = m.id
            )
            ";

        $type_name = $db_Obj->getall($sql);

        //项目
        $sql = "SELECT DISTINCT
            p.project
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.type = b.btype
                    AND p.mifeng = 0
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < ".$time."
                AND p.type = b.btype
            )
            ORDER BY
                p.type ASC,
                b.price ASC ";

        $project = $db_Obj ->getall($sql);
    }else{
        $sql = "SELECT DISTINCT
               p.type
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.mifeng = 0
                    AND p.nphone = '".$phone."'
                    AND p.type = b.btype
                    AND b.mid = m.id
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < '".$time."'
                AND p.nphone = '".$phone."'
                AND p.type = b.btype
                AND b.mid = m.id
            )
            ";

        $type_name = $db_Obj->getall($sql);

        //项目
        $sql = "SELECT DISTINCT
            p.project
            FROM
                t_member AS m,
                t_product AS p,
                t_bid AS b
            WHERE
                (
                    p.mid = '".$mid."'
                    AND p.type = b.btype
                    AND p.mifeng = 0
                )
            OR (
                p.mifeng = 1
                AND p.lasttime < ".$time."
                AND p.mid = '".$mid."'
                AND p.type = b.btype
            )
            ORDER BY
                p.type ASC,
                b.price ASC ";

        $project = $db_Obj ->getall($sql);
    }
    


//类别产品信息
if(!empty($_POST)){
	if(!empty($_POST['type_name'])){
		$type_names=isset($_POST['type_name'])?$_POST['type_name']:'';
		$sql = "SELECT
        	b.btype,
        	b.price,
        	b.bsize,
        	b.delivery,
        	b.MOQ,
        	b.MPQ,
        	b.createtime,
        	b.file_name,
        	b.rate,
        	p.lasttime,
        	p.units,
        	p.currency,
        	p.price_type,
            p.material,
            p.purchase,
            p.plant,
            p.validtime,
            p.invalidtime,
            p.extranum,
        	m.company_name,
        	m.company_data
        FROM
        	t_product AS p,
        	t_bid AS b,
        	t_member AS m
        WHERE
            (
                p.mifeng = 0
                AND p.type = b.btype
                AND b.mid = m.id
                AND p.type = '".$type_names."'
            )
        OR (
            p.mifeng = 1
            AND p.lasttime < '".$time."'
            AND p.type = b.btype
            AND b.mid = m.id
            AND p.type = '".$type_names."'
        );
            ";
		$result = $db_Obj->getall($sql);
	}

	// 项目产品信息
	if(!empty($_POST['project'])){
		$pro = $_POST['project'];
        $sql = "SELECT
            m.company_name,
            m.company_data,
            p.nphone,
            p.project,
            p.lasttime,
            p.units,
            p.currency,
            p.number,
            p.material,
            p.purchase,
            p.plant,
            p.extranum,
            p.validtime,
            p.invalidtime,
            b.price,
            b.btype,
            b.bsize,
            b.delivery,
            b.MOQ,
            b.MPQ,
            b.createtime,
            b.rate
        FROM
            t_member AS m,
            t_product AS p,
            t_bid AS b
        WHERE
            (
                p.mifeng = 0 
                AND p.project = '".$pro."'
                AND b.mid = m.id
                AND p.type = b.btype
            )
        OR (
            p.mifeng = 1
            AND p.lasttime < ".$time."
            AND p.project = '".$pro."'
            AND b.mid = m.id
            AND p.type = b.btype
        )   
        ORDER BY p.type asc, b.price asc
        ";
		$result = $db_Obj->getall($sql);
	}
}



if(!empty($_POST['export'])){
	$export = $_POST['export'];
	if (!empty($_POST['project'])) {
		$pro = $_POST['project'];
	    $sql = "
            SELECT
            m.company_name,
            m.company_data,
            p.nphone,
            p.project,
            p.lasttime,
            p.units,
            p.currency,
            p.price_type,
            p.number,
            p.material,
            p.purchase,
            p.plant,
            p.extranum,
            b.price,
            b.bnumber,
            b.btype,
            b.bsize,
            b.delivery,
            b.MOQ,
            b.MPQ,
            b.createtime,
            b.rate,
            b.bnumber,
            b.file_name
        FROM
            t_member AS m,
            t_product AS p,
            t_bid AS b
        WHERE
             (
                p.mifeng = 0 
                AND p.project = '".$pro."'
                AND b.mid = m.id
                AND p.type = b.btype
            )
        OR (
            p.mifeng = 1
            AND p.lasttime < ".$time."
            AND p.project = '".$pro."'
            AND b.mid = m.id
            AND p.type = b.btype
        )   
        ORDER BY p.type asc, b.price asc
        ";
	    $result = $db_Obj->getall($sql);
	}
    if(!empty($_POST['type_name'])){
    	$sql = "SELECT
        	b.btype,
        	b.price,
        	b.bsize,
        	b.delivery,
        	b.MOQ,
        	b.MPQ,
        	b.createtime,
        	b.file_name,
        	b.rate,
        	b.bnumber,
        	p.lasttime,
        	p.units,
        	p.currency,
        	p.price_type,
        	p.project,
            p.material,
            p.purchase,
            p.plant,
            p.extranum,
        	m.company_name,
        	m.company_data
        FROM
        	t_product AS p,
        	t_bid AS b,
        	t_member AS m
        WHERE
            (
                p.mifeng = 0
                AND p.type = b.btype
                AND b.mid = m.id
                AND p.type = '".$type_names."'
            )
        OR (
            p.mifeng = 1
            AND p.lasttime < '".$time."'
            AND p.type = b.btype
            AND b.mid = m.id
            AND p.type = '".$type_names."'
        );
            ";
		$result = $db_Obj->getall($sql);
    }
    if ($export == 1) {

        $columns = array(
            array(
                'title' => '序号',
                'field' => 'xuhao',
                'width' => 20
            ),
            array(
                'title' => '项目',
                'field' => 'project',
                'width' => 20
            ),
            array(
                'title' => '物料编码',
                'field' => 'btype',
                'width' => 20
            ),
            array(
                'title' => '规格型号',
                'field' => 'bsize',
                'width' => 20
            ),
            array(
                'title' => '供应商',
                'field' => 'company_name',
                'width' => 20
            ),
            array(
                'title' => '供应商代码',
                'field' => 'company_data',
                'width' => 20
            ),
            array(
                'title' => '数量',
                'field' => 'bnumber',
                'width' => 20
            ),
            array(
                'title' => '计量单位',
                'field' => 'units',
                'width' => 20
            ),
            array(
                'title' => '未税价位',
                'field' => 'price',
                'width' => 20
            ),
            array(
                'title' => '税率(%)',
                'field' => 'rate',
                'width' => 20
            ),
            array(
                'title' => '币种',
                'field' => 'currency',
                'width' => 20
            ),
            array(
                'title' => '交期(天)',
                'field' => 'delivery',
                'width' => 20
            ),
            array(
                'title' => 'MOQ(最小订单)',
                'field' => 'MOQ',
                'width' => 20
            ),
            array(
                'title' => 'MPQ(最小包装)',
                'field' => 'MPQ',
                'width' => 20
            ),
            array(
                'title' => '询价类型',
                'field' => 'price_type',
                'width' => 20
            ),
                
            array(
                'title' => '报价截止日期',
                'field' => 'lasttime',
                'width' => 20
            ),
            array(
                'title' => '报价日期',
                'field' => 'createtime',
                'width' =>20
            ),
            array(
                'title' => '物料组',
                'field' => 'material',
                'width' => 20
            ),
            array(
                'title' => '采购组',
                'field' => 'purchase',
                'width' => 20
            ),
            array(
                'title' => '工厂组',
                'field' => 'plant',
                'width' => 20
            ),
            array(
                'title' => '配额',
                'field' => 'extranum',
                'width' => 20
            )
        );

        foreach ($result as $key => $value) {
            $exportlist['xuhao'] =$key+1;
            $exportlist['project'] =$value['project'];
            $exportlist['btype'] =$value['btype'];
            $exportlist['bsize'] =$value['bsize'];
            $exportlist['company_name'] =$value['company_name'];
            $exportlist['company_data'] =$value['company_data'];
            $exportlist['bnumber'] =$value['bnumber'];
            $exportlist['units'] =$value['units'];
            $exportlist['price'] =$value['price'];
            $exportlist['rate'] =$value['rate'];
            $exportlist['currency'] =$value['currency'];
            $exportlist['delivery'] =$value['delivery'];
            $exportlist['MOQ'] =$value['MOQ'];
            $exportlist['MPQ'] =$value['MPQ'];
            $exportlist['price_type'] =$value['price_type'];
            $exportlist['lasttime'] = date('Y-m-d H:i:s',$value['lasttime']);
            $exportlist['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            $exportlist['material'] =$value['material'];
            $exportlist['purchase'] =$value['purchase'];
            $exportlist['plant'] =$value['plant'];
            $exportlist['extranum'] =$value['extranum'];
            $arr[] = $exportlist;
        }
     
       $excObj->export($arr, array(
            "title" => "数据-" . date('Y-m-d-H-i', time()),
            "columns" => $columns
        ));
    }
}

include 'table4.html';
